/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.cggveritas.crewintranet.jms.dao.jpacontroller;

import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.NonexistentEntityException;
import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.PreexistingEntityException;
import fr.cggveritas.crewintranet.jms.entities.CvTypevisitemedicale;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import fr.cggveritas.crewintranet.jms.entities.CvVistemedical;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Alassane
 */
public class CvTypevisitemedicaleJpaController implements Serializable {

    public CvTypevisitemedicaleJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(CvTypevisitemedicale cvTypevisitemedicale) throws PreexistingEntityException, Exception {
        if (cvTypevisitemedicale.getCvVistemedicalList() == null) {
            cvTypevisitemedicale.setCvVistemedicalList(new ArrayList<CvVistemedical>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<CvVistemedical> attachedCvVistemedicalList = new ArrayList<CvVistemedical>();
            for (CvVistemedical cvVistemedicalListCvVistemedicalToAttach : cvTypevisitemedicale.getCvVistemedicalList()) {
                cvVistemedicalListCvVistemedicalToAttach = em.getReference(cvVistemedicalListCvVistemedicalToAttach.getClass(), cvVistemedicalListCvVistemedicalToAttach.getIdvistemedical());
                attachedCvVistemedicalList.add(cvVistemedicalListCvVistemedicalToAttach);
            }
            cvTypevisitemedicale.setCvVistemedicalList(attachedCvVistemedicalList);
            em.persist(cvTypevisitemedicale);
            for (CvVistemedical cvVistemedicalListCvVistemedical : cvTypevisitemedicale.getCvVistemedicalList()) {
                CvTypevisitemedicale oldIdtypevisitemedicaleOfCvVistemedicalListCvVistemedical = cvVistemedicalListCvVistemedical.getIdtypevisitemedicale();
                cvVistemedicalListCvVistemedical.setIdtypevisitemedicale(cvTypevisitemedicale);
                cvVistemedicalListCvVistemedical = em.merge(cvVistemedicalListCvVistemedical);
                if (oldIdtypevisitemedicaleOfCvVistemedicalListCvVistemedical != null) {
                    oldIdtypevisitemedicaleOfCvVistemedicalListCvVistemedical.getCvVistemedicalList().remove(cvVistemedicalListCvVistemedical);
                    oldIdtypevisitemedicaleOfCvVistemedicalListCvVistemedical = em.merge(oldIdtypevisitemedicaleOfCvVistemedicalListCvVistemedical);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findCvTypevisitemedicale(cvTypevisitemedicale.getIdtypevisitemedicale()) != null) {
                throw new PreexistingEntityException("CvTypevisitemedicale " + cvTypevisitemedicale + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(CvTypevisitemedicale cvTypevisitemedicale) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvTypevisitemedicale persistentCvTypevisitemedicale = em.find(CvTypevisitemedicale.class, cvTypevisitemedicale.getIdtypevisitemedicale());
            List<CvVistemedical> cvVistemedicalListOld = persistentCvTypevisitemedicale.getCvVistemedicalList();
            List<CvVistemedical> cvVistemedicalListNew = cvTypevisitemedicale.getCvVistemedicalList();
            List<CvVistemedical> attachedCvVistemedicalListNew = new ArrayList<CvVistemedical>();
            for (CvVistemedical cvVistemedicalListNewCvVistemedicalToAttach : cvVistemedicalListNew) {
                cvVistemedicalListNewCvVistemedicalToAttach = em.getReference(cvVistemedicalListNewCvVistemedicalToAttach.getClass(), cvVistemedicalListNewCvVistemedicalToAttach.getIdvistemedical());
                attachedCvVistemedicalListNew.add(cvVistemedicalListNewCvVistemedicalToAttach);
            }
            cvVistemedicalListNew = attachedCvVistemedicalListNew;
            cvTypevisitemedicale.setCvVistemedicalList(cvVistemedicalListNew);
            cvTypevisitemedicale = em.merge(cvTypevisitemedicale);
            for (CvVistemedical cvVistemedicalListOldCvVistemedical : cvVistemedicalListOld) {
                if (!cvVistemedicalListNew.contains(cvVistemedicalListOldCvVistemedical)) {
                    cvVistemedicalListOldCvVistemedical.setIdtypevisitemedicale(null);
                    cvVistemedicalListOldCvVistemedical = em.merge(cvVistemedicalListOldCvVistemedical);
                }
            }
            for (CvVistemedical cvVistemedicalListNewCvVistemedical : cvVistemedicalListNew) {
                if (!cvVistemedicalListOld.contains(cvVistemedicalListNewCvVistemedical)) {
                    CvTypevisitemedicale oldIdtypevisitemedicaleOfCvVistemedicalListNewCvVistemedical = cvVistemedicalListNewCvVistemedical.getIdtypevisitemedicale();
                    cvVistemedicalListNewCvVistemedical.setIdtypevisitemedicale(cvTypevisitemedicale);
                    cvVistemedicalListNewCvVistemedical = em.merge(cvVistemedicalListNewCvVistemedical);
                    if (oldIdtypevisitemedicaleOfCvVistemedicalListNewCvVistemedical != null && !oldIdtypevisitemedicaleOfCvVistemedicalListNewCvVistemedical.equals(cvTypevisitemedicale)) {
                        oldIdtypevisitemedicaleOfCvVistemedicalListNewCvVistemedical.getCvVistemedicalList().remove(cvVistemedicalListNewCvVistemedical);
                        oldIdtypevisitemedicaleOfCvVistemedicalListNewCvVistemedical = em.merge(oldIdtypevisitemedicaleOfCvVistemedicalListNewCvVistemedical);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = cvTypevisitemedicale.getIdtypevisitemedicale();
                if (findCvTypevisitemedicale(id) == null) {
                    throw new NonexistentEntityException("The cvTypevisitemedicale with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(String id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvTypevisitemedicale cvTypevisitemedicale;
            try {
                cvTypevisitemedicale = em.getReference(CvTypevisitemedicale.class, id);
                cvTypevisitemedicale.getIdtypevisitemedicale();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The cvTypevisitemedicale with id " + id + " no longer exists.", enfe);
            }
            List<CvVistemedical> cvVistemedicalList = cvTypevisitemedicale.getCvVistemedicalList();
            for (CvVistemedical cvVistemedicalListCvVistemedical : cvVistemedicalList) {
                cvVistemedicalListCvVistemedical.setIdtypevisitemedicale(null);
                cvVistemedicalListCvVistemedical = em.merge(cvVistemedicalListCvVistemedical);
            }
            em.remove(cvTypevisitemedicale);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<CvTypevisitemedicale> findCvTypevisitemedicaleEntities() {
        return findCvTypevisitemedicaleEntities(true, -1, -1);
    }

    public List<CvTypevisitemedicale> findCvTypevisitemedicaleEntities(int maxResults, int firstResult) {
        return findCvTypevisitemedicaleEntities(false, maxResults, firstResult);
    }

    private List<CvTypevisitemedicale> findCvTypevisitemedicaleEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(CvTypevisitemedicale.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public CvTypevisitemedicale findCvTypevisitemedicale(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(CvTypevisitemedicale.class, id);
        } finally {
            em.close();
        }
    }

    public int getCvTypevisitemedicaleCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<CvTypevisitemedicale> rt = cq.from(CvTypevisitemedicale.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
